/**
 * Created by archer on 16/4/14.
 */
$(function(){
	MerchantsApp.loadGrid();
});

var MerchantsApp = function(){
	var choose ;
	var shopId ; 
    return {
        loadGrid:function(){
        	gui.widgets.BootStrap.loadGrid({
                gridSelector:"#grid-table",
                pagerSelector:"#grid-pager",
                postData:{
                	name:function(){
                		return $.trim($("#name").val());
                	},
                },
                jsonReader : {
                    root: "data.rows",
                    records: "data.total",
                    repeatitems : false,
                    total: function (obj) {
                        return  Math.ceil(obj.data.total /jQuery("#grid-table").getGridParam("rowNum"));
                    }
                } ,
                datatype: 'json',
                url:$('#APP_ROOT_PATH').val() + 'merchants/page_active',
                colNames:['公司名称','手机号','公众号名称','门店数','代理数','到期时间','操作'],
                colModel:[
                    {name:'name',index:'name', width:60,sortable:false},
                    {name:'phone',index:'phone', width:60,sortable:false},
                    {name:'wxName',index:'wxName', width:60,sortable:false},
                    {name:'shopNum',index:'shopNum', width:50,sortable:false},
                    {name:'totalNum',index:'totalNum', width:50,sortable:false},
                    {name:'gmtValidate',index:'gmtValidate', width:100,sortable:false},
                    {name:'',index:'', width:100,sortable:false,formatter:function(cellvalue,options,rowObject){
                    	var html = "";
                    //	html += "<a href='#' onclick='MerchantsApp.unacticve("+rowObject.shopId+","+rowObject.id+")'>冻结</a>|";
                    	if(rowObject.status == '1')
                    		html += "<a href='#' onclick='MerchantsApp.unacticve("+rowObject.shopId+","+rowObject.id+")'>冻结</a>|";
                    	else
                    		html += "<a href='#' onclick='MerchantsApp.active("+rowObject.shopId+","+rowObject.id+")'>激活</a>|";
                    	if(rowObject.totalNum == '0')
                    		html += " <a href='#' onclick='MerchantsApp.openInput("+rowObject.shopId+",1)'>开通代理</a>";
                    	else
                    		html += " <a href='#' onclick='MerchantsApp.openInput("+rowObject.shopId+",2)'>增加代理</a>";
                    	return html;
                    }},
                ],
                caption: "商家信息",
                rowList:[10,20,30],
                prmNames:{
                    page:"pageNum",
                    rows:"pageSize",
                    sort: "sortField",
                    order: "sortOrder",
                    search:"_search"
                }
            });
        },
        search:function(){
        	jQuery("#grid-table").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
        },
        active:function(id,merchantsId){
        	if(confirm("是否激活")) {
	        	var url = $("#APP_ROOT_PATH").val() + 'merchants/active_m';
				gui.util.Ajax.asyncPOST(url,
				{
				"id": id,
				"merchantsId" : merchantsId,
				},function(res){
					if(res.data){
						jQuery("#grid-table").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
					}else{
						$.gritter.add({
							title: '错误提示',
							text: '删除失败',
							class_name: 'gritter-error'
						});
					}
				});
        	}
			return false;
        },
        unacticve:function(id,merchantsId){
        	if(confirm("是否冻结")) {
	        	var url = $("#APP_ROOT_PATH").val() + 'merchants/unActive_m';
				gui.util.Ajax.asyncPOST(url,
				{
				"id": id,
				"merchantsId" : merchantsId,
				},function(res){
					if(res.data){
						console.log();
						jQuery("#grid-table").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
					}else{
						$.gritter.add({
							title: '错误提示',
							text: '删除失败',
							class_name: 'gritter-error'
						});
					}
				});
        	}
			return false;
        },
        openInput : function(id,cz){
        	shopId = id;
        	choose = cz;
        	htmlstr = "开通代理";
        	if(cz=='2')
        		htmlstr = "增加代理";
        	$("#num").val("");
        	$("#myModalLabel").html(htmlstr);
        	$("#myModal").modal("show");
        },
        sub_form : function(){
        	if(choose == 1)
        		MerchantsApp.openNum(shopId);
        	else
        		MerchantsApp.addNum(shopId);
        },
        openNum : function(id){
        	var url = $("#APP_ROOT_PATH").val() + 'merchants/open_child';
			gui.util.Ajax.asyncPOST(url,
			{
				"shopId": id,
				"addNum" : $("#num").val()
			},function(res){
				if(res.data){
					$("#myModal").modal("hide");
					layer.msg("开通成功",{icon : 1});
					jQuery("#grid-table").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
				}else{
					$.gritter.add({
						title: '错误提示',
						text: '删除失败',
						class_name: 'gritter-error'
					});
				}
			});
        },
        addNum : function(id){
        	var url = $("#APP_ROOT_PATH").val() + 'merchants/add_child';
			gui.util.Ajax.asyncPOST(url,
			{
				"shopId": id,
				"addNum" : $("#num").val()
			},function(res){
				if(res.data){
					$("#myModal").modal("hide");
					layer.msg("增加成功",{icon : 1});
					jQuery("#grid-table").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
				}else{
					$.gritter.add({
						title: '错误提示',
						text: '删除失败',
						class_name: 'gritter-error'
					});
				}
			});
        },
    }
}();